package com.jilin.library.controller;

import com.jilin.library.entity.Reader;
import com.jilin.library.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/readers")
@CrossOrigin
public class ReaderController {
    @Autowired
    private ReaderService readerService;
    
    @GetMapping
    public List<Reader> getAllReaders() {
        return readerService.findAll();
    }
    
    @GetMapping("/{id}")
    public Reader getReader(@PathVariable Integer id) {
        return readerService.findById(id);
    }
    
    @PostMapping
    public Reader addReader(@RequestBody Reader reader) {
        return readerService.addReader(reader);
    }
    
    @PutMapping("/{id}")
    public int updateReader(@PathVariable Integer id, @RequestBody Reader reader) {
        reader.setId(id);
        return readerService.updateReader(reader);
    }
    
    @DeleteMapping("/{id}")
    public int deleteReader(@PathVariable Integer id) {
        return readerService.deleteReader(id);
    }
} 